#!/bin/sh
#
#	Neutron Netns Cleanup OCF RA.
#       Handles the netns cleanup at start / stop of the agent service
#       group
#
# Copyright (c) 2014 Red Hat
#
# This is a one-shot OCF resource agent with the next properties:
#
# * It wraps the init.d script to make an OCF-RA
# * It maps the start, stop, monitor to start, stop , status, and provides
#   the specific OCF ones.
# * It cleans unused resources during start (system or agents startup)
# * It cleans everything on stop (agents migration to other hosts)
# * Once started, it will respond with status = OK
# * Once stopped, it will respond with status = DEAD
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#######################################################################
# Initialization:


: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
: ${OCF_NEUTRON_DIR=${OCF_ROOT}/lib/neutron}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs


WRAPPED_INITD_SCRIPT=${OCF_NEUTRON_DIR}/neutron-netns-cleanup

#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="NetnsCleanup" version="0.9">
<version>1.0</version>

<longdesc lang="en">
This resource agent does nothing during execution, only executes 
a cleanup during start, and a force cleanup during stop of
the netns resources generated by neutron agents.

</longdesc>
<shortdesc lang="en">neutron netns cleanup resource agent</shortdesc>

<parameters>
</parameters>

<actions>
<action name="start"        timeout="40" />
<action name="stop"         timeout="300" />
<action name="monitor"      timeout="20" interval="10" depth="0" />
<action name="reload"       timeout="20" />
<action name="migrate_to"   timeout="20" />
<action name="migrate_from" timeout="20" />
<action name="meta-data"    timeout="5" />
<action name="validate-all"   timeout="20" />
</actions>
</resource-agent>
END
}

#######################################################################


netns_cleanup_usage() {
	cat <<END
usage: $0 {start|stop|monitor|migrate_to|migrate_from|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

netns_cleanup_start() {
    netns_cleanup_monitor
    if [ $? =  $OCF_SUCCESS ]; then
	return $OCF_SUCCESS
    fi
    $WRAPPED_INITD_SCRIPT start && return $OCF_SUCCESS
    return $OCF_ERR_GENERIC
}

netns_cleanup_stop() {
    netns_cleanup_monitor
    if [ $? =  $OCF_SUCCESS ]; then
        $WRAPPED_INITD_SCRIPT stop && return $OCF_SUCCESS
        return $OCF_ERR_GENERIC
    fi
    return $OCF_SUCCESS
}

netns_cleanup_monitor() {
	# Monitor _MUST!_ differentiate correctly between running
	# (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
	# That is THREE states, not just yes/no.

        $WRAPPED_INITD_SCRIPT status && return $OCF_SUCCESS
	
        return $OCF_NOT_RUNNING
}

netns_cleanup_validate() {
    
    return $OCF_SUCCESS
}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
start)		netns_cleanup_start;;
stop)		netns_cleanup_stop;;
monitor)	netns_cleanup_monitor;;
migrate_to)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_target}."
	        netns_cleanup_stop
		;;
migrate_from)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} from ${OCF_RESKEY_CRM_meta_migrate_source}."
	        netns_cleanup_start
		;;
reload)		ocf_log info "Reloading ${OCF_RESOURCE_INSTANCE} ..."
		;;
validate-all)	netns_cleanup_validate;;
usage|help)	netns_cleanup_usage
		exit $OCF_SUCCESS
		;;
*)		netns_cleanup_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc

